<?php

namespace app\admin\controller;

use think\Request;
use think\Controller;

class Order extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $type=input('param.type','1');
        if($type==1){
            $where['a.is_pay']='0';
        }else{
            $where['a.is_pay']='1';
            $where['a.is_show']='1';
        }
        $list=db('order')->alias('a')->field('a.*,b.nickname,c.tname')->
            join("__MEMBER__ b",'b.id=a.member_id','left')->
        join("__ACTIVE__ c",'c.id=a.active_id','left')->
            where($where)->order('a.addtime desc')->paginate(12);
        //echo db('order')->getLastSql();die;
        //dump($list);die;
        return view('order/index',['list'=>$list]);
        //return $this->fetch('login/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $tlist=db('type')->select();
        if($this->request->isPost()){
            $data=input('post.');
            if($data['start_time']){
                $data['start_time']=strtotime($data['start_time']);
            }else{
                $data['start_time']=time();
            }
            if($data['end_time']){
                $data['end_time']=strtotime($data['end_time']);
            }else{
                $data['end_time']=time();
            }
            if($sj=$this->uploadPic()){
                if($sj['code']=='0'){
                    $data['logo']=$sj['data'];
                }else{
                    $this->error('上传失败');
                }
            }
            $data['is_show']=1;
            $data['is_delete']=0;
            $data['type']=0;
            $data['is_view']=1;
            $result=db('active')->insert($data);
            if($result){
                $this->success('添加成功','active/index');
            }else{
                $this->error('添加失败');
            }
        }
        //dump($list);die;
        return view('active/create',['tlist'=>$tlist]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {

    }
    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //$id=$request->param('id');
        $list=db('active')->find($id);
        if($this->request->isPost()){
            $data=input('post.');
            if($data['start_time']){
                $data['start_time']=strtotime($data['start_time']);
            }else{
                $data['start_time']=time();
            }
            if($data['end_time']){
                $data['end_time']=strtotime($data['end_time']);
            }else{
                $data['end_time']=time();
            }
            if($sj=$this->uploadPic()){
                if($sj['code']=='0'){
                    $data['logo']=$sj['data'];
                }else{
                    $this->error('上传失败');
                }
            }
            $result=db('active')->where(['id'=>$id])
                ->update($data);
            if($result!==false){
                if($list['logo']){
                    $path_dir=ROOT_PATH . 'public' . DS . 'uploads/';
                    @unlink($path_dir.$list['logo']);
                }
                $this->success('修改成功','active/index');
            }else{
                $this->error('修改失败');
            }
        }
        //dump($list);die;
        return view('active/update',['list'=>$list]);
    }
    public function uploadPic(){
        $request=Request::instance();
        $file =$request->file('myfile');
        //dump($file);die;
        //移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $path_dir=ROOT_PATH . 'public' . DS . 'uploads/';
            $path_file='active/';
            $info = $file->validate(['size'=>2097152,'ext'=>'jpg,png,gif,jpeg'])->move($path_dir.$path_file);
            if($info){
                $logo_file=$path_file. $info->getSaveName();
                return ['code'=>'0','data'=>$logo_file];
            }else{
                return ['code'=>'10001','data'=>$file->getError()];
                // 上传失败获取错误信息
            }
        }else{
            return [];
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $list=db('order')->find($id);
        if($list['is_pay']==1){
            $result=db('order')->where('id',$id)->update(['is_show','1']);
            if($result!==false){
                $this->success('删除成功','Order/index');
            }else{
                $this->error('删除失败');
            }
        }else{
            $result=db('order')->where('id',$id)->delete();
            if($result!==false){
                $this->success('删除成功','Order/index');
            }else{
                $this->error('删除失败');
            }
        }

    }
}
